package com.iweb.oasys.util;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 封装一个Servlet工具类,用来处理报文
 */
public class ServletUtil {
    /**
     * 返回一个响应报文
     * @param response          响应报文对象
     * @param responseBody      响应体对象
     */
    public static void returnResponse(HttpServletResponse response,ResponseBody responseBody) {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
            String json = JSONObject.toJSONString(responseBody);
            out.write(json);
        }catch (IOException e) {
            e.printStackTrace();
        }finally {
            out.flush();
            out.close();
        }
    }
}
